package com.kischang.blog.web.action;

import com.kischang.blog.model.Review;
import com.kischang.blog.model.Topic;
import com.kischang.blog.model.User;
import com.kischang.blog.service.ReviewService;
import com.kischang.blog.service.TopicService;
import com.kischang.blog.utils.Utils;
import com.kischang.blog.web.actionuser.LoginAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
public class ReviewAction extends ActionSupport {

	private static final long serialVersionUID = -6095123049923168574L;
	@Autowired
	private ReviewService reviewService;
	@Autowired
	private TopicService topicService;
	private String content;
	private int tid;
	private String msg;
	private int stat;
	
	@Override
	public String execute() throws Exception {
		User author = (User) ActionContext.getContext().getSession().get(LoginAction.SESSION_USER);
		if(author == null ){
			stat = 1;
			msg ="您还没有登录，不能发表回复";
			return SUCCESS;
		}
		Topic topic = topicService.getTopicByID(tid);
		if(topic == null ){
			stat = 2;
			msg ="评论的帖子不存在！！！";
			return SUCCESS;
		}
		Review review = new Review();
		review.setAuthorName(author.getNickname());
		review.setContent(content);
		review.setDate(Utils.formatDate());
		review.setUser(author);
		review.setTopic(topic);
		
		topic.setCountReview(topic.getCountReview()+1);
		topicService.update(topic);
		reviewService.save(review);
		stat = 0;
		msg = "评论成功！！！";
		return SUCCESS;
	}

	public void setContent(String content) {
		this.content = content;
	}
	public void setTid(int tid) {
		this.tid = tid;
	}
	public String getMsg() {
		return msg;
	}
	public int getStat() {
		return stat;
	}
}
